Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose quality parameter as part of public API #6

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

Follpvosten
Copy link
Contributor

@Follpvosten Follpvosten commented Mar 26, 2021

this was previously always set to 0.75, but in case library users might want to use custom values for quality_factor, so I am exposing that with this PR. If you don't like my formatting, I can change it back to use longer lines, but then the code won't be rustfmt-formatted anymore (which it was before).

Related question: I will definitely also need a way to losslessly encode; would like to know how you would like to see that exposed in the API. I would probably change the quality_factor to an enum Compression { Lossless, Lossy(f32) }, but am open to other options. I could also add that new API to this PR if you'd like.

@qnighy
Copy link
Owner

qnighy commented Mar 27, 2021

image's high-level API doesn't accept quality parameters and I want to follow the design. How about providing a separate decoding function with a complex configuration? Such a function may accept an extendable option type like WebPDecoderOptions from libwebp (C). Existing APIs like JpegEncoder may be of some help.

@Follpvosten
Copy link
Contributor Author

Follpvosten commented Mar 29, 2021

So I will create a WebpEncoder struct which you can create with a custom quality_factor or not, and it will wrap the current webp_write methods instead of having them be freestanding functions. If it was "just" a custom function, I would have to duplicate every single function to have one variant which includes the parameter and one variant which doesn't.

@Follpvosten
Copy link
Contributor Author

I also added that Compression API now so I can use this crate for lossless compression as well.

C: Deref<Target = [u8]>,
{
let buf = libwebp::WebPEncodeRGBA(&img, img.width(), img.height(), img.width() * 4, 75.0)
pub struct WebpEncoder<W: Write> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could it implement ImageEncoder?

@Follpvosten
Copy link
Contributor Author

Follpvosten commented Apr 18, 2021

Yes, it could; here's a proof of concept implementation.
Note that it only encodes for the color types libwebp directly supports, inspired by how image does it: https://docs.rs/image/0.23.14/src/image/codecs/jpeg/encoder.rs.html#439
I could add something like this to the match color_type with relative ease (16-bit color types are more complicated):

ColorType::L8 => {
    let img = DynamicImage::ImageLuma8(
        ImageBuffer::from_raw(width, height, buf.to_vec()).unwrap(),
    );
    self.encode_rgb(&img.to_rgb8())
}
ColorType::La8 => {
    let img = DynamicImage::ImageLumaA8(
        ImageBuffer::from_raw(width, height, buf.to_vec()).unwrap(),
    );
    self.encode_rgba(&img.to_rgba8())
}

However this might be a hidden cost as we need an allocation (.to_vec()) to create that DynamicImage, so I've left that out for now and opted to stay more close to how image does it.

I would like to document this behavior (and most of the library) in another commit if that's ok, but I can also do a new PR for that if you prefer.

@qnighy
Copy link
Owner

qnighy commented Apr 18, 2021

As you said, following JpegEncoder's behavior seems better.

I would like to document this behavior (and most of the library) in another commit if that's ok

The document would be helpful. Could you go ahead and provide one?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants